/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Core.CoreManager;
import DataAccess.NgonNgu;
import DataAccess.NhaXuatBan;
import java.util.List;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author My PC
 */
public class NgonNguController extends CoreManager<NgonNgu> {
    private String[] columnNames = {"NgonNguID","TenNgonNgu"};
    private String[] clmNames = {"NgonNguID","TenNgonNgu"};
    public NgonNguController()
    { super();}
    @Override
    public void SetcolumnNames()
    {
        super.columnNames=columnNames;
    }
    public void showData(JTable table)
    {
        GetAllData("NgonNgu");
        DefaultTableModel tableModel=new DefaultTableModel(columnNames, 0);
        table.setModel(tableModel);
       // int no=1;
        for(Object o: super.list)
        {
            NgonNgu tg=(NgonNgu)o;
            Vector oneRow=new Vector();
            oneRow.add(tg.getNgonNguId());
            oneRow.add(tg.getTenNgonNgu());
            tableModel.addRow(oneRow);
          //  System.out.println(tableModel.);
        }
    }
    public NgonNgu getNgonNguByIndex(int index)
    {
        return (NgonNgu)list.get(index);
    }
    
      public int GetLastID()
    {
        if(list.size()!=0)
        return ((NgonNgu)this.list.get(list.size()-1)).getNgonNguId();
        else return 0;
        
    }
      public void SearchObject( String[] keywords,JTable table)
    {
        boolean flag=true;
        List<NgonNgu> lstObj;
        String str="from NgonNgu where ";
        for(int i=1;i<keywords.length;i++)
            {
                if(keywords[i].length()!=0)
                {
                    if(!flag) str+=" and ";
                    str+=clmNames[i]+"= :s"+String.valueOf(i);
                    flag=false;
                }
            }
        if(flag) str="from NgonNgu";
        List<NhaXuatBan> lst=null;
        try
        {
            this.session.beginTransaction();
            if(!flag)
            {     
        this.query=this.session.createQuery(str);
        for(int i=1;i<keywords.length;i++)
            {
                if(keywords[i].length()!=0)
                {
                   this.query.setParameter("s"+String.valueOf(i), keywords[i]);
                }
            }
            }
            else
         this.query=this.session.createQuery(str);
        System.out.println(query);
        this.session.getTransaction().commit();
        lst=this.query.list();
    //    System.out.println(lst);
          //  this.session.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
         DefaultTableModel tableModel=new DefaultTableModel(columnNames, 0);
        table.setModel(tableModel);
       // int no=1;
        for(Object o: lst)
        {
            NgonNgu tg=(NgonNgu)o;
            Vector oneRow=new Vector();
            oneRow.add(tg.getNgonNguId());
            oneRow.add(tg.getTenNgonNgu());
            tableModel.addRow(oneRow);
        } 
            }
}

